package com.yonyou.crm.cum.terminalpoint.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yonyou.crm.base.doc.service.BaseDocBizServiceImpl;
import com.yonyou.crm.base.doc.service.BaseDocDetailBizServiceImpl;
import com.yonyou.crm.common.biz.service.BaseBizServiceImpl;
import com.yonyou.crm.common.enumeration.service.EnumBizServiceImpl;
import com.yonyou.crm.common.field.handler.FieldRelationHandler;
import com.yonyou.crm.common.field.relation.EnumFieldRelation;
import com.yonyou.crm.common.field.relation.FieldRelation;
import com.yonyou.crm.common.field.relation.RefFieldRelation;
import com.yonyou.crm.common.page.entity.Page;
import com.yonyou.crm.cum.contact.service.ContactBizServiceImpl;
import com.yonyou.crm.cum.terminalpoint.entity.CumChnlnodeVO;
import com.yonyou.crm.cum.terminalpoint.entity.ManagementModelEnum;
import com.yonyou.crm.cum.terminalpoint.entity.RateEnum;
import com.yonyou.crm.sys.org.service.SysOrgBizServiceImpl;
import com.yonyou.crm.sys.user.service.UserBizServiceImpl;
import com.yonyou.iuap.context.InvocationInfoProxy;

@Service
public class CumChnlnodeBizServiceImpl extends BaseBizServiceImpl<CumChnlnodeVO>{
	
	@Autowired
	private EnumBizServiceImpl enumService;
	@Autowired
	private SysOrgBizServiceImpl orgService;
	@Autowired
	private UserBizServiceImpl userService;
	@Autowired
	private BaseDocBizServiceImpl docService;
	@Autowired
	private BaseDocDetailBizServiceImpl docDetailService;
	@Autowired
	private ContactBizServiceImpl contactService;
	
	public Page<CumChnlnodeVO> getPage(Page<CumChnlnodeVO> page,
			Map<String, Object> paramMap){
		Page<CumChnlnodeVO> cumChnlnodePage = null;
		if (paramMap == null) {
			paramMap = new HashMap<String, Object>();
		}
		paramMap.put("tenantId", InvocationInfoProxy.getTenantid());
		cumChnlnodePage = super.pageSelect(CumChnlnodeVO.class, page, paramMap.keySet().toArray(new String[0]), paramMap.values().toArray(new Object[0]));
		List<CumChnlnodeVO> cumChnlnodeList = cumChnlnodePage.getData();
		for(CumChnlnodeVO cumChnlnode : cumChnlnodeList){
			if(ManagementModelEnum.DISTRIBUTORMANAGE.getValue() == cumChnlnode.getManagementModel()){
				cumChnlnode.setManagementModelName(ManagementModelEnum.DISTRIBUTORMANAGE.getName());
			}else if(ManagementModelEnum.OWNMANAGE.getValue() == cumChnlnode.getManagementModel()){
				cumChnlnode.setManagementModelName(ManagementModelEnum.OWNMANAGE.getName());
			}
			if(RateEnum.WEEK.getValue() == cumChnlnode.getRate()){
				cumChnlnode.setRateName(RateEnum.WEEK.getName());
			}else if(RateEnum.TWOWEEK.getValue() == cumChnlnode.getRate()){
				cumChnlnode.setRateName(RateEnum.TWOWEEK.getName());
			}else if(RateEnum.MONTH.getValue() == cumChnlnode.getRate()){
				cumChnlnode.setRateName(RateEnum.MONTH.getName());
			}
		}
		transCumChnlnode(cumChnlnodeList);
		return cumChnlnodePage;
	}
	
	private List<CumChnlnodeVO> transCumChnlnode(List<CumChnlnodeVO> cumChnlnodeList){
		if(cumChnlnodeList != null && cumChnlnodeList.size() > 0) {
			List<FieldRelation<CumChnlnodeVO>> relations = new ArrayList<FieldRelation<CumChnlnodeVO>>();
			relations.add(new RefFieldRelation<CumChnlnodeVO>(CumChnlnodeVO.class, "deptId", new String[]{"deptName"}, orgService, "selectFieldsByIds"));
			relations.add(new RefFieldRelation<CumChnlnodeVO>(CumChnlnodeVO.class, "orgId", new String[]{"orgName"}, orgService, "selectFieldsByIds"));
			relations.add(new RefFieldRelation<CumChnlnodeVO>(CumChnlnodeVO.class, "typeDetail", new String[]{"typeDetailName"}, docDetailService, "selectFieldsByIds"));
			relations.add(new RefFieldRelation<CumChnlnodeVO>(CumChnlnodeVO.class, "levelDetail", new String[]{"levelDetailName"}, docDetailService, "selectFieldsByIds"));
			relations.add(new RefFieldRelation<CumChnlnodeVO>(CumChnlnodeVO.class, "contactId", new String[]{"contactName"}, contactService, "selectFieldsByIds"));
			relations.add(new RefFieldRelation<CumChnlnodeVO>(CumChnlnodeVO.class, "ownerUserId", new String[]{"ownerUserName"}, userService, "selectFieldsByIds"));
			relations.add(new EnumFieldRelation<CumChnlnodeVO>(CumChnlnodeVO.class, "enableState", "enableStateName", enumService, "enable_state"));
			new FieldRelationHandler<CumChnlnodeVO>().handler(cumChnlnodeList, relations);
		}
		return cumChnlnodeList;
	}
	
	public CumChnlnodeVO save(CumChnlnodeVO cumChnlnode){
//		String contactName = cumChnlnode.getContactName();
//		ContactVO contact = new ContactVO();
//		if(!"".equals(contactName) || null != contactName){
//			contact.setName(contactName);
//			contactService.save(contact);
//		}
//		if(contact.getId() != null) cumChnlnode.setContactId(contact.getId());
		if(ManagementModelEnum.DISTRIBUTORMANAGE.getName().equals(cumChnlnode.getManagementModelName())){
			cumChnlnode.setManagementModel(ManagementModelEnum.DISTRIBUTORMANAGE.getValue());
		}else if(ManagementModelEnum.OWNMANAGE.getName() == cumChnlnode.getManagementModelName()){
			cumChnlnode.setManagementModel(ManagementModelEnum.OWNMANAGE.getValue());
		}
		if(RateEnum.WEEK.getName().equals(cumChnlnode.getRateName())){
			cumChnlnode.setRate(RateEnum.WEEK.getValue());
		}else if(RateEnum.TWOWEEK.getName().equals(cumChnlnode.getRateName())){
			cumChnlnode.setRate(RateEnum.TWOWEEK.getValue());
		}else if(RateEnum.MONTH.getName().equals(cumChnlnode.getRateName())){
			cumChnlnode.setRate(RateEnum.MONTH.getValue());
		}
		return super.saveVO(cumChnlnode);
	}
	
	public int batchDeleteCumChnlnodeByIDs(String[] ids) {
		return dao.batchDelete(CumChnlnodeVO.class, ids);
	}

	public CumChnlnodeVO update(CumChnlnodeVO cumChnlnode) {
		if(ManagementModelEnum.DISTRIBUTORMANAGE.getName().equals(cumChnlnode.getManagementModelName())){
			cumChnlnode.setManagementModel(ManagementModelEnum.DISTRIBUTORMANAGE.getValue());
		}else if(ManagementModelEnum.OWNMANAGE.getName() == cumChnlnode.getManagementModelName()){
			cumChnlnode.setManagementModel(ManagementModelEnum.OWNMANAGE.getValue());
		}
		if(RateEnum.WEEK.getName().equals(cumChnlnode.getRateName())){
			cumChnlnode.setRate(RateEnum.WEEK.getValue());
		}else if(RateEnum.TWOWEEK.getName().equals(cumChnlnode.getRateName())){
			cumChnlnode.setRate(RateEnum.TWOWEEK.getValue());
		}else if(RateEnum.MONTH.getName().equals(cumChnlnode.getRateName())){
			cumChnlnode.setRate(RateEnum.MONTH.getValue());
		}
		return super.updateVO(cumChnlnode);
	}

	public CumChnlnodeVO detail(Long id) {
		CumChnlnodeVO cumChnlnode = super.getVOById(CumChnlnodeVO.class, id);
		List<CumChnlnodeVO> cumChnlnodeList = new ArrayList<CumChnlnodeVO>();
		cumChnlnodeList.add(cumChnlnode);
		transCumChnlnode(cumChnlnodeList);
		cumChnlnode = cumChnlnodeList.get(0);
		return cumChnlnode;
	}
}
